<template>
  <div class="min-h serveProject bg-wt">
    <SearchForm :searchData="pagination" @handleClear="handleClear" @handleReset="handleReset" @handleSearch="getList">
    </SearchForm>
    <TableList :data="data" :pagination="pagination" :bordered="bordered" :hover="hover" :size="size" :total="total"
      @onPageChange="onPageChange" @handleBulid="handleBulid" @handleEdit="handleEdit"></TableList>
    <DialogForm ref="formRef" :visible="visible" :title="title" :data="formDataRef" @handleClose="handleClose"
      @handleEdit="handleEditForm" @handleAdd="handleAdd">
    </DialogForm>
  </div>
</template>

<script setup lang="jsx">
import { onMounted, ref } from 'vue';
import TableList from './components/TableList.vue';
import SearchForm from '../nurse/components/SearchForm.vue';
import DialogForm from './components/DialogForm.vue';
import { getProjectDetails, getProjectList, projectAdd, projectUpdate } from '@/api/serve';
import { MessagePlugin } from 'tdesign-vue-next';
const total = ref(0)
const data = ref([])


const bordered = ref(true);
const hover = ref(false);
const size = ref('medium');


//分页对象
const pagination = ref({
  pageSize: 5,
  pageNum: 1
})
const onPageChange = (pageInfo) => {
  pagination.value.pageNum = pageInfo.current
  pagination.value.pageSize = pageInfo.pageSize
  getList()
}
const getList = async () => {
  const res = await getProjectList(pagination.value)
  if (res.code == 200) {
    data.value = res.data.records
    total.value = res.data.total
  }
}
onMounted(() => {
  getList()
})
const handleClear = (val) => {
  if (val == 'name') {
    pagination.value.name = ''
  } else if (val == 'status') {
    delete pagination.value.status
  }
  getList()
}
const handleReset = () => {
  pagination.value = {
    pageSize: 5,
    pageNum: 1
  }
  getList()
}
const visible = ref(false)
const title = ref("")
const formRef = ref(null)
const handleBulid = () => {
  visible.value = true
  title.value = "新增"
}
const handleClose = () => {
  visible.value = false
}
const handleAdd = async (formData) => {
  const res = await projectAdd(formData)
  //判断是否成功
  if (res.code == 200) {
    //成功
    //提示成功信息
    MessagePlugin.success("新增成功")
    //关闭弹框
    visible.value = false
    //清空form表单数据
    formRef.value.handleClear()
    //重新查询
    getList()
  } else {
    //失败，提示错误信息
    MessagePlugin.error(res.msg)
  }
}
const formDataRef = ref({})
const handleEdit = async (id) => {
  const res = await getProjectDetails(id)
  if (res.code == 200) {
    formDataRef.value = res.data
    visible.value = true
    title.value = '编辑'
  } else {
    MessagePlugin.error(res.msg)
  }
}
const handleEditForm = async (formData) => {
  const res = await projectUpdate(formData)
  //判断是否成功
  if (res.code == 200) {
    //成功
    //提示成功信息
    MessagePlugin.success("修改成功")
    //关闭弹框
    visible.value = false
    //清空form表单数据
    formRef.value.handleClear()
    //重新查询
    getList()
  } else {
    //失败，提示错误信息
    MessagePlugin.error(res.msg)
  }
}
</script>
